package com.fuqi.android.zhongcai.base.view.adapter;


import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

/**
 * create by juner
 * @Date 2016-8-31
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    protected List<T> mDatas = new LinkedList<>();
    public OnItemClickListener<T> mOnItemClickListener;

    public BaseRecyclerViewAdapter(List<T> datas) {
        if (datas != null) {
            mDatas = datas;
        }
    }


    public void addItemList(List<T> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }


    public void remove(int position) {
        mDatas.remove(position);
        notifyDataSetChanged();
    }

    public void removeAll() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (mDatas.size() > 0) {
            count = mDatas.size();
        }
        return count;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public interface OnItemClickListener<T> {

        void onItemClick(View view, int position, T data);

    }

    public void setOnItemClickListener(OnItemClickListener<T> listener) {

        this.mOnItemClickListener = listener;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        return new BaseRecyclerViewHolder(view);
    }

    protected abstract int getLayoutId();

    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {


        final T data = mDatas.get(position);

        BaseRecyclerViewHolder viewHolder = (BaseRecyclerViewHolder) holder;

        onBind(viewHolder.getViewHolder(), position, data);

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v, position, data);
                }
            });
        }

    }


    protected abstract void onBind(ViewHolder holder, int position, T data);

    public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder {
        private ViewHolder viewHolder;

        public BaseRecyclerViewHolder(View itemView) {
            super(itemView);
            viewHolder = ViewHolder.getViewHolder(itemView);
        }

        public ViewHolder getViewHolder() {
            return viewHolder;
        }

    }

    public static class ViewHolder {

        private SparseArray<View> viewHolder;
        private View view;

        private ViewHolder(View view) {
            this.view = view;
            viewHolder = new SparseArray<>();
            view.setTag(viewHolder);
        }

        public <T extends View> T get(int id) {
            View childView = viewHolder.get(id);
            if (childView == null) {
                childView = view.findViewById(id);
                viewHolder.put(id, childView);
            }
            return (T) childView;
        }

        public static ViewHolder getViewHolder(View view) {
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            if (viewHolder == null) {
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }
            return viewHolder;
        }

        public View getConvertView() {
            return view;
        }

        public TextView getTextView(int id) {
            return get(id);
        }

        public ImageView getImageView(int id) {
            return get(id);
        }

        public void setTextView(int id, CharSequence charSequence) {
            getTextView(id).setText(charSequence);
        }

        public void setTextColor(int id, int color) {
            getTextView(id).setTextColor(color);
        }

        public void setBackgroundColor(int id, int color) {
            get(id).setBackgroundColor(color);
        }


    }
}

